<template>
    <view class="index-container">
        <!-- top -->
        <view :id="cmsConfig.Common.IsWxNative ? 'index-topNative' : 'index-top'">
            <view id="index-top-1st" :class="cmsConfig.Common.IsWxNative ? 'index-hint color-grey' : 'index-hint'">您即将进行
            </view>
            <view id="index-top-2nd" :class="cmsConfig.Common.IsWxNative ? 'index-title color-black' : 'index-title'">
                {{ cmsConfig.page.index.clientName }}</view>
            <view :class="cmsConfig.Common.IsWxNative ? 'index-title color-black' : 'index-title'">
                {{ cmsConfig.page.index.businessName }}</view>
            <view :class="cmsConfig.Common.IsWxNative ? 'index-hint color-grey' : 'index-hint'" style="margin-top:13rpx">
                {{ cmsConfig.page.index.certificationCenter }}</view>
        </view>
        <view class="index-auth-protocol">
            <view v-html="cmsConfig.page.index.authorizedProtocol"></view>
        </view>

        <!-- protocol -->
        <view class="protocol-container">
            <view class="index-protocol">
                <checkbox-group @change="checkboxChange"
                    style="display:inline-flex;align-items:center;margin-right:5px;color:#989898;font-size:12px;">
                    <label class="index-protocol-left">
                        <checkbox :color="cmsConfig.Common.IsWxNative ? '#04be02' : '#2d72f1'" :checked="!indexDisableBtn"
                            :value="!indexDisableBtn"></checkbox>我已认真阅读并同意
                    </label>
                </checkbox-group>

                <label id="index-protocol-right" :class="cmsConfig.Common.IsWxNative ? 'color-green' : ''"
                    @click="switchIndexRule">{{ cmsConfig.page.index.protocolTitle }}</label>
            </view>
            <view :class="cmsConfig.Common.IsWxNative ? 'index-btnNative' : 'index-btn'">
                <button type="primary" style="border-radius: 0px" @click="indexToNext" :disabled='indexDisableBtn'
                    :hover-class="cmsConfig.Common.IsWxNative ? 'btn-hoverNative' : 'btn-hover'">
                    {{ cmsConfig.page.index.nextBtnName }}
                </button>
            </view>
            <risk-tips v-if="cmsConfig.Common.RiskTip" />

        </view>



        <!-- {{index_rule}} -->
        <view class="js_dialog" id="iosDialog1" style="opacity: 1;" v-if="index_rule">
            <view class="weui-mask"></view>
            <view class="weui-dialog protocol" style="width:80%">
                <view class="weui-dialog__hd" style="padding:0;margin:0;padding:1.3em 1.6em 0.5em;">
                    <strong class="weui-dialog__title">{{ cmsConfig.protocol.title }}</strong>
                </view>
                <view class="weui-dialog__bd"
                    style="padding:0 0.8em 0.8em;min-height:40px;font-size:15px;line-height:1.3;word-wrap:break-word;word-break:break-all;color:#999999;">

                    <rich-text :nodes="cmsConfig.protocol.clientContent" style="text-indent: 25px;"></rich-text>
                    <view v-if="cmsConfig.protocol.clientContent" class='line'>
                    </view>
                    <view v-if="cmsConfig.protocol.content">
                        <rich-text :nodes="cmsConfig.protocol.content" style="text-indent: 25px;"></rich-text>
                    </view>
                </view>
                <view class="weui-dialog__ft">
                    <view @click="switchIndexRule"
                        :class="cmsConfig.Common.IsWxNative ? 'weui-dialog__btn weui-dialog__btn_primary color-green' : 'weui-dialog__btn weui-dialog__btn_primary'">
                        我知道了</view>
                </view>
            </view>
        </view>

        <view class="js_dialog" id="iosDialog1" style="opacity: 1;" v-if="show_about_dlg">
            <view class="weui-mask"></view>
            <view class="weui-dialog about" style="width:80%">
                <view class="weui-dialog__hd" style="padding:0;margin:0;padding:1.3em 1.6em 0.5em;">
                    <strong class="weui-dialog__title">{{ about.title }}</strong>
                </view>
                <view class="weui-dialog__bd"
                    style="padding:0 0.8em 0.8em;min-height:40px;font-size:15px;line-height:1.3;word-wrap:break-word;word-break:break-all;color:#999999;text-align:left;">
                    <text>{{ cmsConfig.about.content }}</text>
                </view>
                <view class="weui-dialog__ft">
                    <view @click="switchAboutDlg"
                        :class="cmsConfig.Common.IsWxNative ? 'weui-dialog__btn weui-dialog__btn_primary color-green' : 'weui-dialog__btn weui-dialog__btn_primary'">
                        我知道了</view>
                </view>
            </view>
        </view>
    </view>
    <error-toast :isShow="showErrorMsg" :err="err" />
</template>

<script>
import riskTips from "../../components/riskTips/riskTips";
import errorToast from "../../components/errorToast/errorToast";
import { $common, checkNetWork, startNativeVerify } from "../../utils";
import navCus from "../../components/nav-cus/nav-cus";

export default {
    components: {
        riskTips,
        errorToast,
        navCus
    },
    data() {
        return {
            index_rule: false,
            show_about_dlg: false,
            preClickBtnTime: 0, // previous click time, use to prevent click multiple btn enter multiple same page
            indexDisableBtn: true,
            cmsConfig: uni.faceidVerifySdkCmsConfig,
            token: uni.faceidVerifySdkToken,
            baseUrl: uni.faceidVerifySdkBaseUrl,
            curPage: uni.faceidVerifySdkCurPage,
            showErrorMsg: false,
            err: null
        };
    },
    onLoad(options) {
        console.log("protocol page", options);
        this.cmsConfig = uni.faceidVerifySdkCmsConfig;
        this.baseUrl = uni.faceidVerifySdkBaseUrl;
        this.token = uni.faceidVerifySdkToken;
        this.curPage = uni.faceidVerifySdkCurPage;
    },
    methods: {
        handleBack() {
            console.log('uni-back')
            uni.navigateBack();
        },
        checkboxChange(e) {
            this.indexDisableBtn = !this.indexDisableBtn;
            // this.showErrorToast()
        },
        switchIndexRule() {
            this.index_rule = !this.index_rule;
        },
        switchAboutDlg() {
            this.show_about_dlg = !this.show_about_dlg;
        },
        // 按钮防抖，需要改造
        isNotAllowClick: function () {
            var curClickBtnTime = +new Date();
            if (curClickBtnTime - this.preClickBtnTime < 1000) {
                console.log("Click button gap < 1s, not allow exec");
                return true;
            } else {
                this.preClickBtnTime = curClickBtnTime;
                console.log("Click button gap > 1s, allow exec");
                return false;
            }
        },
        // index - goto next page
        async indexToNext() {
            // prevent multi click
            if (this.isNotAllowClick()) {
                return;
            }

            checkNetWork(() => {
                // Confrim that the network is ok, goTo next page
                var cmsConfig = this.cmsConfig;
                // 如果是原生接口,且没有OCR
                if (
                    this.cmsConfig.Common.IsWxNative &&
                    !this.cmsConfig.Common.Flow.includes("Ocr")
                ) {
                    startNativeVerify(this.cmsConfig);
                    return;
                }
                var nextPage = "sms";
                if (cmsConfig.isHideSmsPage) {
                    if (cmsConfig.isHideOcrPage) {
                        nextPage = "verifyLivingBody";
                    } else {
                        nextPage = "ocr";
                    }
                }
                console.log("nextPage", nextPage);
                $common.setCurPage({ url: nextPage });
            }, this.showErrorToast);
        },
        showErrorToast(err) {
            console.log("showErrorToast err=>", err);
            this.showErrorMsg = !this.showErrorMsg;
            this.err = err;
        }
    }
};
</script>

<style scoped>
/* dialog */

.weui-mask {
    position: fixed;
    z-index: 1000;
    top: 0;
    right: 0;
    left: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.8);
}

.weui-dialog {
    position: fixed;
    z-index: 5000;
    width: 75%;
    max-width: 300px;
    top: 50%;
    left: 50%;
    -webkit-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
    background-color: #ffffff;
    text-align: center;
    overflow: inherit;
    border-radius: 5px;
}

.weui-dialog .weui-icon_area {
    position: absolute;
    top: 0;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 0;
}

.weui-dialog__hd {
    padding: 1.3em 1.6em 0.5em;
    padding-top: 50px;
    margin-bottom: 20px;
    line-height: inherit;
}

.weui-dialog__ft {
    position: relative;
    line-height: 48px;
    font-size: 18px;
    border-top: 0.5px solid #ccc;
}

.weui-dialog__btn_primary {
    color: #2d72f1;
}

.weui-dialog .weui-icon_area .iconbg {
    position: absolute;
    top: 10rpx;
    bottom: 10rpx;
    left: 10rpx;
    right: 10rpx;
    background: #fff;
    border-radius: 100%;
}

.weui-dialog.style2 .weui-icon_area .icon-box-img {
    position: relative;
}

.weui-dialog__title {
    font-weight: 400;
    font-size: 18px;
}

.bg-gray {
    background: rgba(155, 155, 155, 0.1);
}

.index-container {
    background: rgba(245, 245, 245, 1);
    display: flex;
    height: 100%;
    flex-direction: column;
    overflow-y: scroll;
}

.protocol-container {
    background-color: white;
    margin-top: auto;
    padding-bottom: 30rpx;
}

#fix-full-page {
    position: fixed;
    top: 0px;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    height: calc(100% - 140px);
}

button[disabled] {
    background: #b3b3b3 !important;
    color: rgba(255, 255, 255, 0.6) !important;
}

#index-top {
    height: 568rpx;
    width: 750rpx;
    text-align: center;
    color: white;
    background: url(https://faceid-verify-temp-1254418846.cos.ap-chengdu.myqcloud.com/images/index-bg.png) no-repeat 0 0 / 100% 100%;
}

#index-topNative {
    height: 568rpx;
    width: 750rpx;
    text-align: center;
    color: white;
    background: url(https://faceid-verify-temp-1254418846.cos.ap-chengdu.myqcloud.com/images/index-bg-native.png) no-repeat 0 0 / 100% 100%;
}

.color-grey {
    color: #a3a3a3 !important;
}

.color-black {
    color: #000 !important;
}

.color-green {
    color: #04be02 !important;
}

.index-hint {
    font-size: 12px;
}

.index-title {
    font-size: 56rpx;
}

#index-top-1st {
    padding-top: 280rpx;
}

#index-top-2nd {
    padding-top: 20rpx;
}

.index-btn {
    margin: 30rpx 30rpx 10rpx 30rpx;
}

.index-btn button {
    background-color: #2d72f1;
    color: white;
}

.index-btnNative {
    margin: 30rpx 30rpx 10rpx 30rpx;
}

.index-btnNative button {
    /* background-color:#2d72f1; */
    color: white;
}

.index-btnNative button[disabled] {
    color: rgba(255, 255, 255, 0.6) !important;
}

.index-btnNative button[disabled][type="primary"] {
    background-color: #9ed99d !important;
}

.weui-dialog.protocol .weui-dialog__bd {
    text-align: left;
    max-height: 300px;
    overflow-y: auto;
}

.line {
    height: 2rpx;
    background: #888;
    margin: 20rpx;
}

.index-auth-protocol {
    background-color: white;
    color: rgba(130, 130, 130, 1);
    opacity: 0.9;
    padding: 32rpx;
}

.index-protocol {
    display: inline-flex;
    align-items: center;
    margin: 16rpx 0 0 30rpx;
    font-size: 12px;
}

.index-protocol-left {
    display: inline-flex;
    color: #989898;
}

.index-protocol checkbox {
    zoom: 0.6;
    width: 35px;
    /* height:20px; */
    margin-top: 2px;
}

#index-protocol-right {
    margin-left: 5px;
    color: #007aff;
    font-size: 14px;
    white-space: nowrap;
}

#index-footer-about {
    font-size: 10px;
    color: #989898;
    margin-top: 8rpx;
    border-bottom: thin solid #2473e9;
}

/* btn hover class, must add !important in mp */
.btn-hover {
    /* background: rgba(45,114,241) !important; */
    background: rgb(11, 89, 235) !important;
}

.btn-hoverNative {
    /* #1AAD19 */
    background: rgba(26, 173, 25, 0.8) !important;
}

.verifyFailWarn {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    margin-bottom: 5px;
    padding: 0 20px;
    text-align: center;
    font-weight: 400;
    font-size: 17px;
}

.verifyFailTop {
    width: 100%;
    margin: 126rpx 0 50rpx 0;
    text-align: center;
}

.page-body-link {
    font-size: 14px;
    color: #007fff;
    text-align: center;
    margin-top: 15px;
}

.verifycamFullScreen {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 100%;
}
</style>
